package com.mornd.gulimall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: mornd
 * @dateTime: 2023/8/8 - 19:41
 */

@Configuration
public class ElasticsearchConfig {

    @Value("${elasticsearch.serverUrl:http://localhost:9200}")
    private String serverUrl;

    public static final RequestOptions COMMON_OPTIONS;

    static  {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();;
        COMMON_OPTIONS = builder.build();
    }

    /**
     * 7.15版本之后已被弃用，使用新推出的 Elasticsearch Java API Client
     *
     * @return
     */
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        return new RestHighLevelClient(
                RestClient.builder(HttpHost.create(serverUrl)));
    }
}
